<template>
  <div class="login-container" :style="backgroundStyle">
    <div class="login-card">
      <div class="card-header">
        <h2>登录系统</h2>
        <p class="subtitle">请登录以访问您的桌面</p>
      </div>
      
      <el-form 
        ref="formRef" 
        :model="loginForm" 
        :rules="loginRules"
        label-position="top"
        @submit.prevent="handleLogin"
      >
        <el-form-item label="用户名" prop="username">
          <el-input 
            v-model="loginForm.username" 
            placeholder="请输入用户名"
            prefix-icon="User"
            @keyup.enter="submitForm"
          />
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input 
            v-model="loginForm.password" 
            type="password" 
            placeholder="请输入密码"
            prefix-icon="Lock"
            show-password
            @keyup.enter="submitForm"
          />
        </el-form-item>
        
        <div class="remember-me">
          <el-checkbox v-model="loginForm.remember">记住我</el-checkbox>
        </div>
        
        <el-form-item>
          <el-button 
            type="primary" 
            :loading="loading" 
            class="login-button"
            @click="submitForm"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="card-footer">
        <p>还没有账号？<el-button type="text" @click="goToRegister">立即注册</el-button></p>
      </div>
    </div>
    
    <div class="login-footer">
      <p>Simple Tools Pro &copy; {{ currentYear }}</p>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue'
import { useRouter } from 'vue-router'
import { User, Lock } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'
import { useAppStore } from '@/stores/app'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()
const appStore = useAppStore()

const loginForm = reactive({
  username: '',
  password: '',
  remember: false
})

const loginRules = reactive<FormRules>({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度应为3-20个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度应为6-20个字符', trigger: 'blur' }
  ]
})

const formRef = ref<FormInstance>()
const loading = ref(false)
const currentYear = computed(() => new Date().getFullYear())

// 背景样式
const backgroundStyle = computed(() => {
  return {
    backgroundImage: `url('/wallpapers/login-bg.jpg')`,
    backgroundSize: 'cover',
    backgroundPosition: 'center'
  }
})

// 提交表单
const submitForm = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate((valid: boolean, fields: any) => {
    if (valid) {
      handleLogin()
    }
  })
}

// 处理登录
const handleLogin = async () => {
  loading.value = true
  
  try {
    // 调用登录接口
    await userStore.login({
      username: loginForm.username,
      password: loginForm.password,
      remember: loginForm.remember
    })
    
    // 初始化应用数据
    await appStore.initAppData()
    
    // 登录成功，跳转到桌面
    router.push('/workspace')
  } catch (error: any) {
    ElMessage.error(error.message || '登录失败，请稍后再试')
  } finally {
    loading.value = false
  }
}

// 跳转到注册页面
const goToRegister = () => {
  router.push('/register')
}
</script>

<style scoped>
.login-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  position: relative;
}

.login-card {
  width: 400px;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 8px;
  padding: 30px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
  backdrop-filter: blur(10px);
}

.card-header {
  text-align: center;
  margin-bottom: 30px;
}

.card-header h2 {
  margin: 0;
  font-size: 28px;
  color: var(--el-color-primary);
}

.subtitle {
  color: #606266;
  margin-top: 10px;
}

.remember-me {
  margin-bottom: 20px;
}

.login-button {
  width: 100%;
  padding: 12px 0;
  font-size: 16px;
}

.card-footer {
  margin-top: 20px;
  text-align: center;
  color: #606266;
}

.login-footer {
  position: absolute;
  bottom: 20px;
  color: white;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}
</style> 